home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / local / sbin / udev-watchdog-wrapper < prev    next >
Encoding:
Text File  |  2013-01-10  |  2.9 KB  |  90 lines

  1. #!/bin/sh
  2.  
  3. set -e
  4.  
  5. ### Helper functions
  6.  
  7. # For whatever reason, the initscript that calls us sets a pretty scarse $PATH
  8. PATH="/usr/bin:${PATH}"
  9.  
  10. ### Helper functions
  11.  
  12. using_fromiso() {
  13.     grep -qs -w -E '(fromiso|isofrom)=' /proc/cmdline
  14. }
  15.  
  16. # Returns the boot device's path in a form that can be passed to the
  17. # eject command, e.g. /dev/scd0 or /dev/block/NN:MM.
  18. boot_device() {
  19.     if using_fromiso ; then
  20.     # When booting with e.g. fromiso=/dev/sdx3/tails-XXX.iso, a loop device
  21.     # is mounted onto /live/image => we cannot get the boot device from there.
  22.     # This loop device's backing file is seen by the system as
  23.     # /isofrom/XXX.iso, which is not available presumably because pivotroot
  24.     # was run => we cannot get the boot device from there either.
  25.     # Instead, we parse fromiso='s argument the same way live-boot does
  26.     # in order to extract the device path (/dev/sdx3)
  27.         for ARGUMENT in $(cat /proc/cmdline) ; do
  28.             case "${ARGUMENT}" in
  29.                 isofrom=*|fromiso=*)
  30.                     FROMISO="${ARGUMENT#*=}"
  31.                     ;;
  32.             esac
  33.         done
  34.         echo $(dirname "$FROMISO")
  35.     else
  36.     # Refactorer, beware: the rest of this script depends on the fact that
  37.     # the path returned in this case is suitable to be passed as an argument
  38.     # to --path in "udevadm info --query" commands... which is not the case
  39.     # of paths in the /dev/sdxN form.
  40.         DEV_NUMBER="$(udevadm info --device-id-of-file=/live/image)"
  41.         echo "/dev/block/$DEV_NUMBER"
  42.     fi
  43. }
  44.  
  45. # First clean the screen, then brutally shutdown the machine.
  46. do_stop() {
  47.     # Really make sure that the CD is ejected
  48.     # FIXME: this might not be necessary with future kernel/udev
  49.     if [ "${DEV_TYPE}" = "cd" ]; then
  50.         /usr/bin/eject -i off "${BOOT_DEVICE}" 2>&1 >/dev/null || true
  51.         /usr/bin/eject -m "${BOOT_DEVICE}" 2>&1 >/dev/null || true
  52.     fi
  53.  
  54.     /usr/bin/pkill gdm3 2>&1 >/dev/null || true
  55.     # TODO-Wheezy: after Squeeze, kbd is replaced by console-tools,
  56.     # and chvt is now shipped in /usr/bin/chvt (adapt memlockd.cfg too)
  57.     /bin/chvt 1
  58.     /etc/init.d/kexec-load stop 2>&1 >/dev/null || true
  59.     /etc/init.d/tails-kexec stop 2>&1 >/dev/null || true
  60. }
  61.  
  62.  
  63. ### Main
  64.  
  65. BOOT_DEVICE=$(boot_device)
  66.  
  67. # Assign to QUERY_SELECTOR an option that can be passed as a query selector
  68. # to udevadm info --query commands.
  69. if using_fromiso ; then
  70.     DEV_NAME=$(basename "$BOOT_DEVICE")
  71.     QUERY_SELECTOR="--name $DEV_NAME"
  72. else
  73.     QUERY_SELECTOR="--path $BOOT_DEVICE"
  74. fi
  75.  
  76. DEV_UDEV_PATH=$(udevadm info --query path     $QUERY_SELECTOR)
  77. DEV_TYPE_LINE=$(udevadm info --query property $QUERY_SELECTOR | grep -w '^ID_TYPE')
  78. DEV_TYPE="${DEV_TYPE_LINE#*=}"
  79.  
  80. # If the world was sane we'd want to *disable* the eject lock, but it turns out
  81. # that blocks the block events so udev-watchdog never receives the "change"
  82. # event. See [[bugs/sdmem_on_eject_broken_for_CD]].
  83. # FIXME: we might be able to do the more sane "-i off" with future kernel/udev
  84. if [ "$DEV_TYPE" = "cd" ]; then
  85.     eject -i on "${BOOT_DEVICE}" 2>&1 >/dev/null
  86. fi
  87.  
  88. # Start udev-watchdog and stop on clean exit.
  89. /usr/local/sbin/udev-watchdog "$DEV_UDEV_PATH" "$DEV_TYPE" && do_stop
  90.